﻿using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Text;
using Dataglot;
using IrishSettings.TableControllers;
using NUnit.Framework;

namespace IrishSettings.IntegrationTests.TableControllerTests
{
    [TestFixture]
    public class ApplicationAndVersionControllerTests
    {
        [Test]
        public void Select()
        {
            DataFactory factory = new DataFactory(ConfigUtils.OleDbConnectionString(), ConfigUtils.Provider());
            using (DbConnection con = factory.Connection())
            {
                TransactionManager manager = new TransactionManager(con);
                manager.Begin();
                ApplicationAndVersionController applicationController = new ApplicationAndVersionController(factory, con, manager.Transaction);
                SettingsApplication app = applicationController.Insert(CreateASampleAppliction());

                applicationController.Select(app.Id);
            }
        }

        [Test]
        public void Insert()
        {
            DataFactory factory = new DataFactory(ConfigUtils.OleDbConnectionString(), ConfigUtils.Provider());
            using (DbConnection con = factory.Connection())
            {
                TransactionManager manager = new TransactionManager(con);
                manager.Begin();
                ApplicationAndVersionController applicationController = new ApplicationAndVersionController(factory, con, manager.Transaction);
                SettingsApplication app = applicationController.Insert(CreateASampleAppliction());

                Assert.Greater(app.Id,0);
            }
        }

        [Test]
        public void Update()
        {
            DataFactory factory = new DataFactory(ConfigUtils.OleDbConnectionString(), ConfigUtils.Provider());
            using (DbConnection con = factory.Connection())
            {
                TransactionManager manager = new TransactionManager(con);
                manager.Begin();
                ApplicationAndVersionController applicationController = new ApplicationAndVersionController(factory, con, manager.Transaction);
                SettingsApplication app = applicationController.Insert(CreateASampleAppliction());

                app.MachineName = "Joe";
                applicationController.Update(app);
            }
        }

        [Test]
        public void Delete()
        {
            DataFactory factory = new DataFactory(ConfigUtils.OleDbConnectionString(), ConfigUtils.Provider());
            using (DbConnection con = factory.Connection())
            {
                TransactionManager manager = new TransactionManager(con);
                manager.Begin();
                ApplicationAndVersionController applicationController = new ApplicationAndVersionController(factory, con, manager.Transaction);
                SettingsApplication app = applicationController.Insert(CreateASampleAppliction());

                applicationController.Delete(app.Id);
            }
        }

        [Test]
        public void DoesNotExist()
        {
            DataFactory factory = new DataFactory(ConfigUtils.OleDbConnectionString(), ConfigUtils.Provider());
            using (DbConnection con = factory.Connection())
            {
                TransactionManager manager = new TransactionManager(con);
                manager.Begin();
                ApplicationAndVersionController applicationController = new ApplicationAndVersionController(factory, con, manager.Transaction);
                applicationController.Insert(CreateASampleAppliction());

               Assert.IsFalse(applicationController.Exists(99999999));
            }
        }

        [Test]
        public void Exists()
        {
            DataFactory factory = new DataFactory(ConfigUtils.OleDbConnectionString(), ConfigUtils.Provider());
            using (DbConnection con = factory.Connection())
            {
                TransactionManager manager = new TransactionManager(con);
                manager.Begin();
                ApplicationAndVersionController applicationController = new ApplicationAndVersionController(factory, con, manager.Transaction);
                SettingsApplication app = applicationController.Insert(CreateASampleAppliction());

                Assert.IsTrue( applicationController.Exists(app.Id));
            }
        }


        private SettingsApplication CreateASampleAppliction()
        {
            SettingsApplication setting = new SettingsApplication();
            setting.Application = "IntegrationTests";
            setting.Culture = "en-US";
            setting.Version = "1.0";
            setting.CurrentVersion = "1.0";
            return setting;
        }
    }
}
